package zust.se.delicious.service.impl;

import java.sql.Timestamp;
import java.util.LinkedList;
import java.util.List;

import zust.se.delicious.dao.BookmarkDao;
import zust.se.delicious.dao.FollowDao;
import zust.se.delicious.dao.UserDao;
import zust.se.delicious.domain.Follow;
import zust.se.delicious.domain.User;
import zust.se.delicious.service.UserService;
import zust.se.delicious.util.WebUtil;
import zust.se.delicious.vo.FollowUserBean;
import zust.se.delicious.vo.PageBean;
import zust.se.delicious.vo.RegisterUserBean;
import zust.se.delicious.vo.SessionUserBean;
import zust.se.delicious.vo.UserBean;

public class UserServiceImpl implements UserService {

	private UserDao userDao;
	private FollowDao followDao;
	private BookmarkDao bookmarkDao;

	public void setFollowDao(FollowDao followDao) {
		this.followDao = followDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	public SessionUserBean login(String username, String password) {
		User user = userDao.findByNameAndPass(username, WebUtil.codePassword(password));
		if (user != null) {
			SessionUserBean sessionUserBean = new SessionUserBean();
			sessionUserBean.setId(user.getId());
			sessionUserBean.setUsername(user.getUsername());
			return sessionUserBean;
		}
		return null;
	}

	@Override
	public boolean register(RegisterUserBean registUser) {
		User user = getUser(registUser);
		if (userDao.save(user) > 0) {
			return true;
		}
		return false;
	}

	private User getUser(RegisterUserBean registUser) {
		User user = new User();
		user.setUsername(registUser.getUsername());
		user.setPassword(WebUtil.codePassword(registUser.getPassword()));
		user.setEmail(registUser.getEmail());
		user.setRegisterTime(new Timestamp(System.currentTimeMillis()));
		return user;
	}

	private User getUser(FollowUserBean followUser) {
		User user = new User();
		user.setId(followUser.getId());
		return user;
	}

	public boolean follow(FollowUserBean followUser) {
		User user = getUser(followUser);
		Follow follow = null;
		if (followDao.save(follow) > 0) {
			return true;
		}
		return false;
	}

	@Override
	public void updateUserInfo(User user) {
		User storeUser = userDao.get(user.getId());
		if (user.getAvatar() != null)
			storeUser.setAvatar(user.getAvatar());
		if (user.getEmail() != null)
			storeUser.setEmail(user.getEmail());
		if (user.getIntroduce() != null)
			storeUser.setIntroduce(user.getIntroduce());
		userDao.update(storeUser);
	}

	@Override
	public UserBean getHomeUser(int uid) {
		User user = userDao.get(uid);
		UserBean userBean = convertToBean(user);
		userBean.setFollowNum(followDao.countFollow(uid).intValue());
		userBean.setFollowingNum(followDao.countFollowing(uid).intValue());
		userBean.setBookmarkNum(bookmarkDao.countBmByOwnId(uid));
		return userBean;
	}

	public UserBean convertToBean(User user) {
		UserBean userBean = new UserBean();
		userBean.setId(user.getId());
		userBean.setRegisterTime(user.getRegisterTime().getTime());
		userBean.setUsername(user.getUsername());
		userBean.setEmail(user.getEmail());
		userBean.setIntroduce(user.getIntroduce());
		userBean.setAvatar(user.getAvatar());
		return userBean;
	}

	public void setBookmarkDao(BookmarkDao bookmarkDao) {
		this.bookmarkDao = bookmarkDao;
	}

	@Override
	public boolean isFollow(Integer uid, Integer fid) {
		Follow follow = new Follow();
		follow.setFollower(new User(uid));
		follow.setFollowing(new User(fid));
		return followDao.isFollow(follow);
	}

	@Override
	public boolean follow(Integer uid, Integer fid) {
		Follow follow = new Follow();
		follow.setFollower(new User(uid));
		follow.setFollowing(new User(fid));
		return followDao.save(follow) > 0;
	}

	@Override
	public boolean unfollow(Integer uid, Integer fid) {
		return followDao.delete(uid, fid);
	}

	@Override
	public List<UserBean> getFollowingList(int uid, PageBean pageBean) {
		pageBean.setTotal(followDao.countFollowing(uid).intValue());
		List<User> uList = followDao.getFollowingList(uid, pageBean.getCurrent(), pageBean.getSize());
		List<UserBean> ubList = new LinkedList<UserBean>();
		for (User user : uList) {
			UserBean userBean = convertToBean(user);
			userBean.setFollowAction("unfollow");
			ubList.add(userBean);
		}
		return ubList;
	}

	@Override
	public List<UserBean> getFollowerList(int uid, PageBean pageBean) {
		pageBean.setTotal(followDao.countFollow(uid).intValue());
		System.out.println(pageBean);
		List<User> uList = followDao.getFollowerList(uid, pageBean.getCurrent(), pageBean.getSize());
		List<UserBean> ubList = new LinkedList<UserBean>();
		for (User user : uList) {
			UserBean userBean = convertToBean(user);
			userBean.setFollowAction(!isFollow(uid, user.getId()) ? "follow" : "unfollow");
			ubList.add(userBean);
		}
		return ubList;
	}

	@Override
	public boolean valPassword(int uid, String oldPassword, String newPassword) {
		User user = userDao.get(uid);
		if (user.getPassword().equals(WebUtil.codePassword(oldPassword))) {
			user.setPassword(WebUtil.codePassword(newPassword));
			userDao.update(user);
			return true;
		}
		return false;
	}
}
